package com.example.service.shopping;


import cn.hutool.core.util.ObjectUtil;
import com.example.common.enums.RoleEnum;
import com.example.mapper.shopping.CartMapper;
import com.example.model.entity.other.Account;
import com.example.model.entity.shopping.Cart;
import com.example.model.vo.CartVO;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 购物车信息表业务处理
 **/
@Service
public class CartService {

    @Resource
    private CartMapper cartMapper;

    /**
     * 新增
     */
    public void add(Cart cart) {
        Account currentUser = TokenUtils.getCurrentUser();
        Cart dbcart=new Cart();
        if (RoleEnum.USER.name().equals(currentUser.getRole())) {
            cart.setUserId(currentUser.getId());
            if(cart.getNum()==null){
                cart.setNum(0);
            }
            dbcart = cartMapper.selectByUserIdAndMedicinesId(currentUser.getId(), cart.getMedicinesId(),cart.getMedicinesStyleId());
        }
        if (RoleEnum.DOCTOR.name().equals(currentUser.getRole())) {
            cart.setDoctorId(currentUser.getId());
            if(cart.getNum()==null){
                cart.setNum(0);
            }
            dbcart = cartMapper.selectByDoctorIdAndMedicinesId(currentUser.getId(), cart.getMedicinesId(),cart.getMedicinesStyleId());
        }
        // 判断该用户对该商品有没有加入过购物车，如果加入过，那么只要更新一下该条记录的num（+1）
        if (ObjectUtil.isNotEmpty(dbcart)) {
            dbcart.setNum(dbcart.getNum() + 1);
            cartMapper.updateById(dbcart);
        } else {
            cartMapper.insert(cart);
        }
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        cartMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            cartMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Cart Cart) {
        cartMapper.updateById(Cart);
    }

    /**
     * 根据ID查询
     */
    public Cart selectById(Integer id) {
        return cartMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<CartVO> selectAll(Cart Cart) {
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            Cart.setUserId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            Cart.setDoctorId(currentUser.getId());
        }
        return cartMapper.selectAll(Cart);
    }

    /**
     * 分页查询
     */
    public PageInfo<CartVO> selectPage(Cart Cart, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Account currentUser = TokenUtils.getCurrentUser();
        if(RoleEnum.USER.name().equals(currentUser.getRole())){
            Cart.setUserId(currentUser.getId());
        }else if(RoleEnum.DOCTOR.name().equals(currentUser.getRole())){
            Cart.setDoctorId(currentUser.getId());
        }
        List<CartVO> list = cartMapper.selectAll(Cart);
        return PageInfo.of(list);
    }

}
